package com.compass.doctor.ui.mine.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.R
import com.compass.doctor.databinding.FragmentBaseListWhiteBinding
import com.compass.doctor.ui.mine.adapter.WalletDetailAdapter
import com.compass.doctor.ui.mine.viewmodel.IntegralViewModel
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.ui.mine.IncomeDetailsActivity
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.getDrawableResource
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 钱包明细fragment
 * Date:2023/12/25 22:43
 */
class WalletDetailFragment : BaseVBFragment<FragmentBaseListWhiteBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private val integralViewModel: IntegralViewModel by viewModels()
    private lateinit var mAdapter: WalletDetailAdapter
    private var mPage = 1
    private var orderType: Int = 1//1-收入 2-支出
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            orderType = it.getInt(KEY_ORDER_TYPE)
        }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@WalletDetailFragment)
            setOnLoadMoreListener(this@WalletDetailFragment)
        }
        mAdapter = WalletDetailAdapter()
        mBinding?.recyclerView?.apply {
            background= getDrawableResource(R.color.white)
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }

        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)
            IncomeDetailsActivity.start(requireContext(),item)
        }
    }



    override fun initData() {
        integralViewModel.walletListLiveData.observe(this) {
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
        getDoctorWalletDetail()
    }


    private fun getDoctorWalletDetail() {
        integralViewModel.getDoctorWalletDetail( mPage, orderType)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getDoctorWalletDetail()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getDoctorWalletDetail()
    }
}